package cn.itcast.controller;

import cn.itcast.pojo.*;
import cn.itcast.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;
    //分页查询学生信息
    @GetMapping
    public Result findByPage(QueryString queryString){
        PageInfo<Student> stu = studentService.findBypage(queryString);

        return stu != null ? Result.success(stu):Result.erro("查询失败");
    }
    //添加学员
    @PostMapping
    public Result add(@RequestBody Student student){
        boolean s = studentService.add();

        return s ? Result.success(s) :Result.erro("添加失败");
    }
    //批量删除学员
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable int[] ids){
        boolean result = studentService.delete(ids);

        return result ? Result.success("删除成功") : Result.erro("删除失败");
     }
     //根据id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Student result = studentService.findById(id);
        log.info("结果是:{}",result);
        return result != null ? Result.success(result) : Result.erro("查找失败");
    }
    //修改学员信息
    @PutMapping
    public Result updata(@RequestBody Student student){
        boolean result = studentService.update(student);

        return result ? Result.success("修改成功") :Result.erro("修改失败");
    }
    //违纪
    @PutMapping("/violation/{id}/{score}")
    public Result updates(@PathVariable("id") Integer id,@PathVariable("score") Integer score){
        boolean updates = studentService.updates(id,score);

        return updates ? Result.success("修改成功") : Result.erro("修改失败");
    }

}
